local zhanmeng = fk.CreateSkill {

  name = "joy__zhanmeng",

  tags = {  },

}



zhanmeng:addEffect(fk.CardUsing, {
  name = "joy__zhanmeng",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(zhanmeng.name) then
      local room = player.room
      local mark = player:getMark("joy__zhanmeng_last-turn")
      if type(mark) ~= "table" then
        mark = {}
        local logic = room.logic
        local current_event = logic:getCurrentEvent()
        local all_turn_events = logic.event_recorder[GameEvent.Turn]
        if type(all_turn_events) == "table" then
          local index = #all_turn_events
          if index > 0 then
            local turn_event = current_event:findParent(GameEvent.Turn)
            if turn_event ~= nil then
              index = index - 1
            end
            if index > 0 then
              current_event = all_turn_events[index]
              current_event:searchEvents(GameEvent.UseCard, 1, function (e)
                table.insertIfNeed(mark, e.data[1].card.trueName)
                return false
              end)
            end
          end
        end
        room:setPlayerMark(player, "joy__zhanmeng_last-turn", mark)
      end
      return (player:getMark("joy__zhanmeng1-turn") == 0 and not table.contains(mark, data.card.trueName)) or
        player:getMark("joy__zhanmeng2-turn") == 0 or (player:getMark("joy__zhanmeng3-turn") == 0 and
        not table.every(room.alive_players, function (p)
          return p == player or p:isNude()
        end))
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getMark("joy__zhanmeng_last-turn")
    local choices = {}
    event:setCostData(self, {})
    if player:getMark("joy__zhanmeng1-turn") == 0 and not table.contains(mark, data.card.trueName) then
      table.insert(choices, "joy__zhanmeng1")
    end
    if player:getMark("joy__zhanmeng2-turn") == 0 then
      table.insert(choices, "joy__zhanmeng2")
    end
    local targets = {}
    if player:getMark("joy__zhanmeng3-turn") == 0 then
      for _, p in ipairs(room.alive_players) do
        if p ~= player and not p:isNude() then
          table.insertIfNeed(choices, "joy__zhanmeng3")
          table.insert(targets, p.id)
        end
      end
    end
    table.insert(choices, "Cancel")
    local choice = room:askForChoice(player, choices, zhanmeng.name, "#joy__zhanmeng-choice", false,
    {"joy__zhanmeng1", "joy__zhanmeng2", "joy__zhanmeng3", "Cancel"})
    if choice == "Cancel" then return false end
    event:getCostData(self)[1] = choice
    if choice == "joy__zhanmeng3" then
      local to = room:askForChoosePlayers(player, targets, 1, 1, "#joy__zhanmeng-choose", zhanmeng.name, true)
      if #to > 0 then
        event:getCostData(self)[2] = to[1]
      else
        return false
      end
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self)[1]
    room:setPlayerMark(player, choice.."-turn", 1)
    if choice == "joy__zhanmeng1" then
      local cards = {}
      for _, id in ipairs(room.draw_pile) do
        if not Fk:getCardById(id).is_damage_card then
          table.insertIfNeed(cards, id)
        end
      end
      if #cards > 0 then
        local card = table.random(cards)
        room:moveCards({
          ids = {card},
          to = player.id,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = player.id,
          skillName = zhanmeng.name,
        })
      end
    elseif choice == "joy__zhanmeng2" then
      room:setPlayerMark(player, "joy__zhanmeng_delay-turn", data.card.trueName)
    elseif choice == "joy__zhanmeng3" then
      local p = room:getPlayerById(event:getCostData(self)[2])
      local cards = room:askForDiscard(p, 2, 2, true, zhanmeng.name, false, ".", "#joy__zhanmeng-discard:"..player.id)
      local x = Fk:getCardById(cards[1]).number
      if #cards == 2 then
        x = x + Fk:getCardById(cards[2]).number
      end
      if x > 10 and not p.dead then
        room:damage{
          from = player,
          to = p,
          damage = 1,
          damageType = fk.FireDamage,
          skillName = zhanmeng.name,
        }
      end
    end
  end,
})
zhanmeng:addEffect(fk.TurnEnd, {
  refresh_events = {fk.TurnEnd},
  can_refresh = Util.TrueFunc,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@joy__zhanmeng_delay", player:getMark("joy__zhanmeng_delay-turn"))
  end,
})

zhanmeng:addEffect(fk.CardUseFinished, {
  name = "#joy__zhanmeng_delay",
  anim_type = "drawcard",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    return player:usedSkillTimes(zhanmeng.name) == 0 and player:getMark("@joy__zhanmeng_delay") == data.card.trueName
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = {}
    for _, id in ipairs(room.draw_pile) do
      if Fk:getCardById(id).is_damage_card then
        table.insertIfNeed(cards, id)
      end
    end
    if #cards > 0 then
      local card = table.random(cards)
      room:moveCards({
        ids = {card},
        to = player.id,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        proposer = player.id,
        skillName = zhanmeng.name,
      })
    end
  end,
})

return zhanmeng